﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using System.Web.UI.HtmlControls;
using GCELibrary.Current;
using ZF.Model;
using System.Transactions;
using ZF.BLL.Page;

namespace ZF.WS.info
{
    public partial class pFitup : PageBase
    {
        


        /// 报告生成器
        /// </summary>
        public override WarnFactory WarnFactory
        {
            get
            {
                return new WarnFactory("/warn.aspx");

            }
        }
        /// <summary>
        /// 1为装修2为商城
        /// </summary>
        [PageArgument(Key = "tid",CanNull= false)]
        public int TypeId { get; set; }
        /// <summary>
        /// 当前页面的操作行为
        /// </summary>
        [PageArgument(Key = "ot", DefaultValue = PageOperType.AddNew)]
        public override PageOperType OperType { get; set; }
        [PageArgument(Key = "id", CanNull=false, BindOperType=PageOperType.Edit)]
        public int Id { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                {
                    Add();
                }
                else if (this.OperType == PageOperType.Edit)
                {
                    Edit();
                }
            }
        }
        public void InitControls()
        {
      
        }


        public void Add()
        {
            InitControls();


            //var member = SiteInfo.GetCurrentMember();
            //if (member != null)
            //{
            //    this.tbContact.Text = member.FullName;
            //    this.tbEmail.Text = member.EMail;
            //    this.tbQQ.Text = member.QQ;
            //    this.tbPhone.Text = member.Phone;
            //    this.tbAddress.Text = member.Address;
            //}

        }
        public void AddSubmit(object sender, EventArgs e)
        {
            var member = SiteInfo.GetCurrentMember();
            using (var ts = new TransactionScope())
            {
                using (var dc = SiteInfo.CreateDBDataContext())
                {
                    var fitup = new Model.I_Fitup()
                    {
                       
                        AddTime = DateTime.Now,
                      
                        Contact = this.tbContact.Text,
                        Email = this.tbEmail.Text,
                    
                        Phone = this.tbPhone.Text,
                        TypeId=this.TypeId,
                        Title = this.tbTitle.Text,
                  
                        Content = this.FCKeditor1.Value,
                  
                        MemberId = member == null ? 0 : member.MemberId
                        
                    };
                    SiteInfo.CheckFilterWords(fitup);
                    try
                    {
                        dc.I_Fitup.InsertOnSubmit(fitup);
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.Current,
                        Message = "发布成功",
                        DoPage = string.Format("/info/fitup.aspx?id={0}&tid={1}", fitup.FitupId.ToString(), TypeId),
                    }).Alert();
                    return;


                    //string msgKey = "信息";
                    //SiteInfo.InsertLog("发布" + msgKey, null);
                    //WarnMessageLink link = new WarnMessageLink(string.Format("成功发布一个{1}{0}，点击确定按钮返回上一页", msgKey, this.ddlType.SelectedItem.Text));
                    //link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                    //    string.Format("~/info/view.aspx?id={0}", fitup.FitupId));
                    //link.AddWarnMessageLink(string.Format("继续发布{0}", msgKey),
                    //    Request.Url.ToString());
                    //var fromUrl = this.ViewState["fromUrl"];
                    //if (fromUrl != null)
                    //{
                    //    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, fromUrl.ToString()).Alert();
                    //}
                    //else
                    //{
                    //    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.GoBack).Alert();
                    //}

                }

            }


        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var member = SiteInfo.GetCurrentMember();
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                I_Fitup fitup = null;
                if(member.UserType== UserType.Member)
                    fitup = dc.I_Fitup.FirstOrDefault(p => p.FitupId == this.Id && p.FitupId == member.MemberId);
                else
                    fitup = dc.I_Fitup.FirstOrDefault(p => p.FitupId == this.Id);
                if (fitup == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
 
                InitControls();  
                 this.tbEmail.Text=fitup.Email;
 
 
                this.tbTitle.Text=fitup.Title ;
     
                this.tbContact.Text = fitup.Contact;
 
                this.tbTitle.Text = fitup.Title;
                this.tbPhone.Text = fitup.Phone;

                this.FCKeditor1.Value = fitup.Content;
           

    

            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var member = SiteInfo.GetCurrentMember();
                    if (member == null)
                        throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                    I_Fitup fitup = null;
                    if (member.UserType == UserType.Member)
                        fitup = dc.I_Fitup.FirstOrDefault(p => p.FitupId == this.Id && p.FitupId == member.MemberId);
                    else
                        fitup = dc.I_Fitup.FirstOrDefault(p => p.FitupId == this.Id);
                    if (fitup == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
         
                    fitup.Title = this.tbTitle.Text;
                    fitup.Phone = this.tbPhone.Text;
 
                    fitup.Email = this.tbEmail.Text;
                    fitup.Content = this.FCKeditor1.Value;

                    fitup.Contact = this.tbContact.Text;
                 

                    SiteInfo.CheckFilterWords(fitup);

                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }

                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.Close,
                        Message = "修改成功",

                    }).Alert();
                    return;

                    //string msgKey = "信息";

                    //SiteInfo.InsertLog("修改" + msgKey, null);
                    //WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{1}{0}，点击确定按钮返回{0}列表", msgKey, this.ddlType.SelectedItem.Text));
                    //link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                    //    string.Format("/info/view.aspx?id={0}", info.InfoId));
                    //link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey),
                    //    Request.Url.ToString());
                    //var fromUrl = this.ViewState["fromUrl"];

                    //if (fromUrl != null)
                    //{
                    //    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, fromUrl.ToString()).Alert();
                    //}
                    //else
                    //{
                    //    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.GoBack).Alert();
                    //}









                }


            }

        }

    }
}
